package com.yupi.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.court.AddCourtRequest;
import com.yupi.springbootinit.model.dto.court.CourtQueryRequest;
import com.yupi.springbootinit.model.dto.court.UpdataCourtRequest;
import com.yupi.springbootinit.model.entity.Court;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.service.CourtService;
import com.yupi.springbootinit.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 场地管理接口。给
 * @Author xingyu Xue
 * @Date 2024/4/20 15:18
 */
@RestController
@RequestMapping("/court")
@Slf4j
@ApiOperation("场馆管理接口")
public class CourtController {
    @Resource
    private UserService userService;

    @Autowired
    private CourtService courtService;
    @Autowired
    private UserService service;

    /**
     * 新增场地
     * @param addCourtRequest
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/add")
    public BaseResponse addCourt(@RequestBody AddCourtRequest addCourtRequest){
        if (addCourtRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String courtName = addCourtRequest.getCourtName();
        String courtProvince = addCourtRequest.getCourtProvince();
        String courtCity = addCourtRequest.getCourtCity();
        String courtAddress = addCourtRequest.getCourtAddress();
        String phoneNumber = addCourtRequest.getPhoneNumber();
        Integer courtArea = addCourtRequest.getCourtArea();
        String courtMain = addCourtRequest.getCourtMain();
        String businessSatus = addCourtRequest.getBusinessSatus();
        Boolean result = courtService.addCourt(courtName, courtProvince, courtCity, courtAddress, phoneNumber, courtArea, courtMain, businessSatus);

        if (result){
            return ResultUtils.success(result);
        }else throw new BusinessException(ErrorCode.SYSTEM_ERROR);

    }

    /**
     * 查询所有场地信息
     * @param request
     * @return
     */
    @GetMapping("/all")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<Court>> getAllCourt(HttpServletRequest request){
        if (request == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(courtService.getAllCourt());
    }

    /**
     * 修改指定场馆信息
     * @param updataCourtRequest
     * @param request
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/updata")
    public BaseResponse<Boolean> updataCourt(@RequestBody UpdataCourtRequest updataCourtRequest, HttpServletRequest request){

        if (updataCourtRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        log.info("用户id{}修改具体信息如下",loginUser.getId());
        log.info("id:{}",updataCourtRequest.getId());
        log.info("courtName:{}",updataCourtRequest.getCourtName());
        log.info("courtProvince:{}",updataCourtRequest.getCourtProvince());
        log.info("courtAddress:{}",updataCourtRequest.getCourtAddress());
        log.info("phoneNumber:{}",updataCourtRequest.getPhoneNumber());
        log.info("courtArea:{}",updataCourtRequest.getCourtArea());
        log.info("courtMain:{}",updataCourtRequest.getCourtMain());
        log.info("businessSatus:{}",updataCourtRequest.getBusinessSatus());


        return  ResultUtils.success(courtService.updataCourt(updataCourtRequest));


    }

    /**
     * 删除指定场馆
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteCourt(long id,HttpServletRequest request){
        log.info("======用户id为：{}=====场馆编号为{}===========",userService.getLoginUser(request).getId(),id);
        if (request == null || id <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        log.info("用户id{}删除场馆信息，场馆编号为{}",loginUser.getId(),id);
        return ResultUtils.success(courtService.deleteCourt(id));
    }

    /**
     * 分页查询场馆
     * @param courtQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Court>> listCourtByPage(@RequestBody CourtQueryRequest courtQueryRequest, HttpServletRequest request){
        if (request == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        log.info("用户id为{}的用户，查询场馆信息",loginUser.getId());
        long current = courtQueryRequest.getCurrent();
        long pageSize = courtQueryRequest.getPageSize();
        Page<Court> page = courtService.page(new Page<>(current, pageSize));

        return ResultUtils.success(page);
//        return ResultUtils.success(courtService.listCourtByPage(courtQueryRequest));
    }





    @GetMapping("/get")
    public BaseResponse<Court> getCourtById(long id, HttpServletRequest request){
        if (request == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        log.info("用户id为{}的用户，查询场馆信息，场馆编号为{}",loginUser.getId(),id);
        return ResultUtils.success(courtService.getById(id));
    }





}
